//
//  SubMemberListViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/6.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SubMemberListViewController.h"
#import "SubGroupTableViewCell.h"
#import "SubGroupModel.h"
@interface SubMemberListViewController ()

@property(nonatomic,strong)UIButton *selectAllBtn;

@property(nonatomic,copy)NSString*SearchKeyWord;

@end

@implementation SubMemberListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.homeTableView.estimatedRowHeight=50;
    
    [self.homeTableView registerNib:[UINib nibWithNibName:@"SubGroupTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    [self.homeTableView.mj_header beginRefreshing];
    
      [self.view addSubview:self.selectAllBtn];
    
    // Do any additional setup after loading the view.
}


-(UIButton*)selectAllBtn
{
    if (!_selectAllBtn) {

        _selectAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _selectAllBtn.frame = CGRectMake(0, 0, 60, 20);
        
        [_selectAllBtn setTitle:@"全选" forState:UIControlStateNormal];
        
        [_selectAllBtn setTitle:@"取消" forState:UIControlStateSelected];
        
        _selectAllBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [_selectAllBtn sizeToFit];
        
        _selectAllBtn.tintColor=[UIColor clearColor];
        
        [_selectAllBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_selectAllBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:_selectAllBtn];
        
        self.navigationItem.rightBarButtonItem=item;
        
        [_selectAllBtn addTarget:self action:@selector(GotoEdit:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _selectAllBtn;
}


-(void)GotoEdit:(UIButton*)sender
{
    sender.selected=!sender.selected;
    
    for (SubGroupData *data in self.dataArr) {
        
        data.Seleclt=sender.selected;
        
    }
    
    [self.homeTableView reloadData];
    
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubGroupData *data=self.dataArr[indexPath.row];
    
    SubGroupTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.isClick=YES;
    
    cell.deleStatue=YES;//是否编辑状态
    
    cell.model=data;
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubGroupData *data=self.dataArr[indexPath.row];
    
    data.Seleclt=!data.Seleclt;
    
    [self.homeTableView reloadData];
    
}


-(void)addNewgoods
{
   
    
    NSMutableArray *groupTypeIdArr=[NSMutableArray array];
    
    for (SubGroupData *data in self.dataArr) {
        if (data.Seleclt) {
            
            [groupTypeIdArr addObject:[NSString stringWithFormat:@"%ld",data.ID]];
        }
    }
    
    if (groupTypeIdArr.count==0) {
        
        [MBProgressHUD showWarnMessage:@"至少选择一项"];
        
        return;
    }
    
     [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/groupItemAdd.json" andrequestTime:5 andParams:@{@"groupId":self.Reqmodel.groupId,@"staffId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]],@"groupType":self.Reqmodel.groupType,@"groupTypeIds":[HelpWay arrToJSON:groupTypeIdArr]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        if (responseStatus==1) {
            
            [MBProgressHUD showSuccessMessage:@"新增成功"];
            
            _block();
            
            [self.navigationController popViewControllerAnimated:YES];
       
        }else if (responseStatus==0)
        {
            
            [MBProgressHUD showErrorMessage:message];
            
        }else if (responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
        }else{
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
    }];

}

#pragma mark-跳转事件
-(void)PushChangeStatueVC
{
  
    BaseSearchChooseViewController *Choose=[[BaseSearchChooseViewController alloc]init];
    
    
    if ([self.Reqmodel.groupType isEqualToString:@"1"]) {
        //商户
        
        Choose.ReqStr=[SearchAndChooseManager getChooseUrlWithType:ShopListType];
        
        Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:ShopListType andIsLookSelf:YES];
        
    }else{
        //检查员
        Choose.ReqStr=@"";
        
        Choose.CustomArr=@[];
        
    }
    
    
    Choose.SearchPlace=self.searchName;
    
    Choose.KeyWord=self.SearchKeyWord;
    
    Choose.SureBlock=^(NSString *paras,NSString*keyword)
    {
        self.SearchKeyWord=keyword;
        
        self.Reqmodel.params=paras;
        
        [self.homeTableView.mj_header beginRefreshing];
    };
    
    [self.navigationController pushViewController:Choose animated:YES];
    
    
}




#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
    
}


/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{
    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/groupItemList.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        SubGroupModel *InforModel=[SubGroupModel mj_objectWithKeyValues:responseObject];
        
        
        if (responseStatus==1) {
            //请求成功

            NSString* showStr=@"";
            
            if ([self.Reqmodel.groupType isEqualToString:@"1"]) {
                
                showStr=@"商户";
                
            }else{
                
                showStr=@"检查员";
                
            }
            
            self.TotalNum=[NSString stringWithFormat:@"一共找到%ld个%@",InforModel.totalRecord,showStr];
            
            if (InforModel.data.count==0&&self.Reqmodel.currentPage==1) {
                
                [self.dataArr removeAllObjects];
                
                [self.homeTableView reloadData];
                
                self.Type=TBNODateType;
        
            }else if (InforModel.data.count==0&&self.Reqmodel.currentPage!=1){
         
                [self.homeTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else{
         
                if (self.Reqmodel.currentPage==1) {
                    
                    [self.dataArr removeAllObjects];
                    
                }
                
                [self.dataArr addObjectsFromArray:InforModel.data];
                
                [self.homeTableView reloadData];
                
            }
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
